package com.bnz.text;

import com.alibaba.druid.pool.DruidDataSource;
import com.bnz.pojo.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

public class TestSpring {

    private ClassPathXmlApplicationContext ac;

    @Before
    public void init(){

        /*ac=new ClassPathXmlApplicationContext("spring/applicationContext.xml");*/
        /*ac=new ClassPathXmlApplicationContext("spring/beans.xml");*/
       /* ac=new ClassPathXmlApplicationContext("spring/collectionBeans.xml");*/
        ac=new ClassPathXmlApplicationContext("spring/loadProperties.xml");

    }


    /*1、使用默认的无参的构造方法创建对象*/
    @Test
    public void text01(){

        Student student1= (Student) ac.getBean("student1");
        System.out.println(student1);

    }

    /* <!-- 2\通过静态方法构造学生对象-->*/
    @Test
    public void text02(){

        Student student2= (Student) ac.getBean("student2");
        System.out.println(student2);

    }

    /* <!--3\通过工厂创建学生对象-->*/
    /*@Test
    public void text03(){
        Student student3= (Student) ac.getBean("student3");
        System.out.println(student3);
    }*/

    @Test
    public void text04(){
        Student stud4= (Student) ac.getBean("stud4");
        System.out.println(stud4);
    }

    @Test
    public void text05(){
        Student stud5= (Student) ac.getBean("stud5");
        System.out.println(stud5);
    }

    @Test
    public void text06(){
        Student stud6= (Student) ac.getBean("stud6");
        System.out.println(stud6);
    }

    @Test
    public void test07(){
        Student stud7= (Student) ac.getBean("stud7");
        System.out.println(stud7);
    }

    @Test
    public void test08(){
        Object cb= ac.getBean("cb");
        System.out.println(cb);
    }

    //测试单例还是多例
    @Test
    public void test09(){
        Object student1=  ac.getBean("student1");
        Object student2=  ac.getBean("student1");

        System.out.println(student1);
        System.out.println(student2);

    }


    //加载连接池
    @Test
    public void test10() throws SQLException {
        DruidDataSource dataSource = (DruidDataSource) ac.getBean("dataSource");
        String driverClassName = dataSource.getDriverClassName();
        String url = dataSource.getUrl();
        String username = dataSource.getUsername();
        String password = dataSource.getPassword();

        System.out.println("driverClassName = " + driverClassName);
        System.out.println("url = " + url);
        System.out.println("username = " + username);
        System.out.println("password = " + password);



        System.out.println("connection = " + dataSource.getConnection());

    }




    @After
    public void after(){

        ac.close();
    }

}
